<?php
namespace \Absidon\Http\Transport;
/**
 * Component controls for type hinting and ensuring proper transport method support.
 *
 * @author Jacob Santos <webmaster@absidongames.com>
 * @package Absidon
 * @subpackage HTTP_Transport
 * @since 0.1
 * @license Simplified BSD License <http://www.opensource.org/licenses/bsd-license.html>
 * @license GNU Lesser General Public License 2.1 or later <http://www.opensource.org/licenses/lgpl-2.1.php>
 */

/**
 * Interface for supporting transports. The HTTP class will only take class that have this component.
 *
 * @since 0.1
 */
abstract class Component
{
	/**
	 * HTTP request method.
	 *
	 * @since 0.1
	 * @var string
	 */
	protected $method = 'GET';

	/**
	 * HTTP request URL.
	 *
	 * @since 0.1
	 * @var string
	 */
	protected $url = '';

	/**
	 * HTTP options.
	 *
	 * @since 0.1
	 * @var \Absidon\Http\Options
	 */
	protected $options = null;

	/**
	 * HTTP headers.
	 *
	 * @since 0.1
	 * @var \Absidon\Http\Headers
	 */
	protected $headers = null;

	/**
	 * The HTTP body for the request.
	 *
	 * @since 0.1
	 * @var string
	 */
	protected $body = '';

	/**
	 * Whether connection should be blocking or async (false).
	 *
	 * @since 0.1
	 * @var boolean
	 */
	protected $blocking = true;

	/**
	 * Whether the transport is supported by PHP and the configurations.
	 *
	 * @since 0.1
	 *
	 * @return boolean True if it can be used, false if it will fail.
	 */
	abstract static public function is_supported();

	/**
	 * Send the request to get a response.
	 *
	 * @since 0.1
	 *
	 * @return \Absidon\Http\Response
	 */
	abstract public function request();

	/**
	 * Set the HTTP request method.
	 *
	 * @since 0.1
	 *
	 * @param string $method Optional, default is GET. All supported HTTP protocol methods.
	 */
	public function method($method = 'GET')
	{
		$this->method = strtoupper($method);
	}

	/**
	 * Set the URL for the request.
	 *
	 * @since 0.1
	 * @todo Validate and sanitize URL.
	 *
	 * @param string $url URL to send request.
	 */
	public function url($url)
	{
		$this->url = $url;
	}

	/**
	 * Set extra options for the HTTP request.
	 *
	 * @since 0.1
	 *
	 * @param Options $options Options that may or may not be supported on every transport.
	 */
	public function options(\Absidon\Http\Options $options)
	{
		$this->options = $options;
	}

	/**
	 * Set the headers for the request.
	 *
	 * @since 0.1
	 *
	 * @param Headers $headers Extra headers to send.
	 */
	public function headers(\Absidon\Http\Headers $headers)
	{
		$this->headers = $headers;
	}

	/**
	 * Sets the body to be set.
	 *
	 * This is the raw body, it should already be url-encoded if sending form or could be
	 * url-encoded automatically, if the header supports it.
	 *
	 * @since 0.1
	 * @todo Support compression.
	 *
	 * @param string $body
	 * @param bool $compress Optional, default is false (unsupported). Whether to compress body in request.
	 */
	public function body($body, $compress = false)
	{
		$this->body = $body;
	}

	/**
	 * Whether the connection will not block.
	 *
	 * This is not supported on all transports and some transports that support it, don't implement
	 * it correctly.
	 *
	 * @since 0.1
	 */
	public function disable_blocking()
	{
		$this->blocking = false;
	}
}